package com.neusoft.tyut.myspring.mybatisdemo.mapper.impl;

import com.neusoft.tyut.myspring.mybatisdemo.entity.Dept;
import com.neusoft.tyut.myspring.mybatisdemo.mapper.BaseMapper;
import com.neusoft.tyut.myspring.mybatisdemo.mapper.DeptMapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeptMapperImpl extends BaseMapper implements DeptMapper {
    @Override
    public Dept findDeptByDeptno(Integer deptno) throws SQLException {
        String sql = " "; // select * from dept where deptno = ?
        Object[] args = {deptno};
        List<Map<String, Object>> query = super.query(sql, args);
        Dept dept = toDept(query).get(0);
        return dept;
    }

    private List<Dept> toDept(List<Map<String, Object>> query) {
        List<Dept> depts = new ArrayList<>();
        for (Map<String, Object> map : query) {
            Dept dept = new Dept();
            dept.setDeptno((Integer) map.get("deptno"));
            dept.setDname((String) map.get("dname"));
            dept.setLoc((String) map.get("loc"));
            depts.add(dept);
        }
        return depts;
    }
}
